package cn.gemiman.daoyi.module.cms.service.appsitealbumphoto;

import cn.gemiman.daoyi.module.cms.dal.dataobject.appsitealbum.AppSiteAlbumDO;
import cn.gemiman.daoyi.module.cms.dal.mysql.appsitealbum.AppSiteAlbumMapper;
import cn.gemiman.daoyi.module.cms.service.appsitealbum.AppSiteAlbumService;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.gemiman.daoyi.module.cms.controller.admin.appsitealbumphoto.vo.*;
import cn.gemiman.daoyi.module.cms.dal.dataobject.appsitealbumphoto.AppSiteAlbumPhotoDO;
import cn.gemiman.daoyi.framework.common.pojo.PageResult;
import cn.gemiman.daoyi.framework.common.pojo.PageParam;
import cn.gemiman.daoyi.framework.common.util.object.BeanUtils;

import cn.gemiman.daoyi.module.cms.dal.mysql.appsitealbumphoto.AppSiteAlbumPhotoMapper;

import static cn.gemiman.daoyi.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.gemiman.daoyi.module.cms.enums.ErrorCodeConstants.*;

/**
 * 相册照片 Service 实现类
 *
 * @author 道一源码
 */
@Service
@Validated
public class AppSiteAlbumPhotoServiceImpl implements AppSiteAlbumPhotoService {

    @Resource
    private AppSiteAlbumPhotoMapper appSiteAlbumPhotoMapper;
    @Resource
    private AppSiteAlbumMapper appSiteAlbumMapper;

    @Override
    public Long createAppSiteAlbumPhoto(AppSiteAlbumPhotoSaveReqVO createReqVO) {
        // 插入
        AppSiteAlbumPhotoDO appSiteAlbumPhoto = BeanUtils.toBean(createReqVO, AppSiteAlbumPhotoDO.class);
        AppSiteAlbumDO appSiteAlbumDO = appSiteAlbumMapper.selectById(appSiteAlbumPhoto.getAlbumId());
        appSiteAlbumPhoto.setAppId(appSiteAlbumDO.getAppId());
        appSiteAlbumPhoto.setAppSiteId(appSiteAlbumDO.getAppSiteId());
        appSiteAlbumPhotoMapper.insert(appSiteAlbumPhoto);
        // 返回
        return appSiteAlbumPhoto.getId();
    }

    @Override
    public void updateAppSiteAlbumPhoto(AppSiteAlbumPhotoSaveReqVO updateReqVO) {
        // 校验存在
        validateAppSiteAlbumPhotoExists(updateReqVO.getId());
        // 更新
        AppSiteAlbumPhotoDO updateObj = BeanUtils.toBean(updateReqVO, AppSiteAlbumPhotoDO.class);
        appSiteAlbumPhotoMapper.updateById(updateObj);
    }

    @Override
    public void deleteAppSiteAlbumPhoto(Long id) {
        // 校验存在
        validateAppSiteAlbumPhotoExists(id);
        // 删除
        appSiteAlbumPhotoMapper.deleteById(id);
    }

    private void validateAppSiteAlbumPhotoExists(Long id) {
        if (appSiteAlbumPhotoMapper.selectById(id) == null) {
            throw exception(APP_SITE_ALBUM_PHOTO_NOT_EXISTS);
        }
    }

    @Override
    public AppSiteAlbumPhotoDO getAppSiteAlbumPhoto(Long id) {
        return appSiteAlbumPhotoMapper.selectById(id);
    }

    @Override
    public PageResult<AppSiteAlbumPhotoDO> getAppSiteAlbumPhotoPage(AppSiteAlbumPhotoPageReqVO pageReqVO) {
        return appSiteAlbumPhotoMapper.selectPage(pageReqVO);
    }

}
